package com.hundredapp.oa.web.service;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Repository;

import com.hundredapp.oa.dao.SelectType;
import com.hundredapp.oa.domain.OaClass;
import com.hundredapp.oa.domain.OaContract;
import com.hundredapp.oa.domain.OaElect;
import com.hundredapp.oa.domain.OaStudent;
import com.hundredapp.oa.domain.OaSubject;
import com.hundredapp.oa.repository.ClassmngManager;
import com.hundredapp.oa.repository.ContractManager;
import com.hundredapp.oa.repository.ElectManager;
import com.hundredapp.oa.repository.SignManager;
import com.hundredapp.oa.repository.StudentManager;
import com.hundredapp.oa.repository.SubjectManager;
import com.hundredapp.oa.repository.UserManager;
import com.hundredapp.oa.util.JSEscape;
import com.hundredapp.oa.util.WebUtil;
import com.hundredapp.oa.vo.SubjectVO;
import com.hundredapp.oa.vo.TermVO;
import com.opensymphony.xwork2.ActionSupport;

@Repository
@Scope("prototype")
public class Student extends ActionSupport {

	@Autowired
	private StudentManager studentManager;

	@Autowired
	private SubjectManager subjectManager;

	@Autowired
	private ContractManager contractManager;

	@Autowired
	private UserManager userManager;

	@Autowired
	private ClassmngManager classmng;

	@Autowired
	private SignManager signmng;

	@Autowired
	private ElectManager electmng;

	private List<OaStudent> root;

	private List<SubjectVO> subjects;

	private OaContract payment;

	private Integer studentid;

	private Integer classid;

	private OaStudent student = new OaStudent();

	private Object success;

	private String msg;

	private int limit;

	private int start;

	private int total;

	private String query;

	private String query2;

	private String query3;

	private String query4;

	private Integer leslesson;

	private List<String> contracts = new ArrayList<String>(0);

	private Double sumoverage;
	private Integer sumlesson;
	private List<TermVO> terms;

	public String allstu() {

		String condition = SelectType.SELECT_ALL;

		if (query != null) {
			query = JSEscape.unescape(query);
			condition += " and displayName like '%" + query + "%' ";
		}

		if (query2 != null) {
			query2 = JSEscape.unescape(query2);
			condition += " and phone like '%" + query2 + "%' ";
		}

		if (query3 != null) {
			query3 = JSEscape.unescape(query3);
			condition += " and school like '%" + query3 + "%' ";
		}

		if (query4 != null) {
			query4 = JSEscape.unescape(query4);
			condition += " and emergency like '%" + query4 + "%' ";
		}

		root = studentManager.find(condition, start, limit);
		total = studentManager.count(condition);

		return SUCCESS;
	}

	public String updatestu() {
		List<OaContract> cs = new ArrayList<OaContract>(0);
		try {

			for (String each : contracts) {
				OaContract c = contractManager.findById(Integer.parseInt(each));
				cs.add(c);
			}

			student.setOaContracts(cs);
			studentManager.update(student);
			success = true;
			msg = "保存成功";
		} catch (Exception e) {
			e.printStackTrace();
			success = false;
			msg = "保存出现异常";
		}

		return SUCCESS;
	}

	public String find() {
		student = studentManager.findById(studentid);
		return SUCCESS;
	}

	public String add() {
		List<OaContract> cs = new ArrayList<OaContract>(0);

		try {
			student.setCreateUserId(WebUtil.GetLoginUserId());

			for (String each : contracts) {
				OaContract c = contractManager.findById(Integer.parseInt(each));
				cs.add(c);
			}
			student.setOaContracts(cs);
			studentManager.save(student);
		} catch (Exception e) {
			success = false;
			msg = "保存出现异常";
		}
		success = true;
		msg = "保存成功";
		return SUCCESS;
	}

	public String updatepay() {
		List<OaStudent> ss = new ArrayList<OaStudent>(0);
		if (!(studentid == null)) {
			ss.add(studentManager.findById(studentid));
		}
		payment.setOaStudents(ss);
		payment.setCreateUser(userManager.findUserById(WebUtil.GetLoginUserId()));
		payment.setState(1);
		contractManager.update(payment);
		success = true;
		msg = "更新成功";
		return SUCCESS;
	}

	public String findpresentations() {
		subjects = new ArrayList<SubjectVO>();
		List<OaContract> contracts = studentManager.findById(studentid)
				.getOaContracts();
		for (OaContract con : contracts) {
			for (OaElect elect : electmng.findByContract(con.getId())) {
				if (elect.getIspre() == 0) {
					OaSubject sub = elect.getOaSubject();
					SubjectVO subvo = new SubjectVO();
					subvo.setDescription(sub.getDescription());
					subvo.setDisplayName(sub.getDisplayName());
					subvo.setId(sub.getId());
					subvo.setLesson((int) (elect.getPrice() / (sub.getPrice() / sub
							.getLesson())));
					subvo.setPrice(sub.getPrice());
					switch (sub.getSemester()) {
					case 1:
						subvo.setSemesters("第一学期");
						break;
					case 2:
						subvo.setSemesters("第二学期");
						break;
					case 3:
						subvo.setSemesters("第三学期");
						break;
					case 4:
						subvo.setSemesters("第四学期");
						break;
					}
					subjects.add(subvo);
				}
			}
		}
		return SUCCESS;
	}

	public String overage() {
		terms = new ArrayList<TermVO>();
		TermVO t1 = new TermVO();
		TermVO t2 = new TermVO();
		TermVO t3 = new TermVO();
		TermVO t4 = new TermVO();
		terms.add(t1);
		terms.add(t2);
		terms.add(t3);
		terms.add(t4);

		sumoverage = 0.0;
		sumlesson = 0;
		student = studentManager.findById(studentid);
		for (OaContract c : student.getOaContracts()) {
			
			for (OaElect e : c.getElects()) {
				Double diff = e.getPrice() - e.getOaSubject().getPrice();
				Double unit = e.getOaSubject().getPrice()/e.getOaSubject().getLesson();
				sumoverage += diff;
				Integer less = (int) Math.ceil(diff/unit);
				sumlesson += less;
			}
								
		}
		
		for(OaClass c : student.getOaClasses()) {
			OaSubject sub = c.getOaSubject();
			Integer t = sub.getSemester() - 1;
			
			
			terms.get(t).setSum(terms.get(t).getSum() + sub.getPrice());
			terms.get(t).setLesson(terms.get(t).getLesson() + sub.getLesson());
			
			SubjectVO vo = new SubjectVO();
			vo.setDisplayName(sub.getDisplayName());
			String condition = SelectType.SELECT_ALL;
			condition += " and studentId=" + studentid;
			condition += " and classId="+classmng.findBySub(sub.getId()).getId();
			Integer count = signmng.count(condition);
			vo.setLesson(count);
			vo.setPrice(count * (sub.getPrice()/sub.getLesson()));
			
			terms.get(t).getSubjects().add(vo);
		}	
		
		
		if(sumoverage < 0) sumoverage = 0.0;
		if(sumlesson < 0) sumlesson = 0;
		success = true;
		return SUCCESS;
	}

	public List<OaStudent> getRoot() {
		return root;
	}

	public void setStudentManager(StudentManager studentManager) {
		this.studentManager = studentManager;
	}

	public OaContract getPayment() {
		return payment;
	}

	public void setPayment(OaContract payment) {
		this.payment = payment;
	}

	public Object getSuccess() {
		return success;
	}

	public String getMsg() {
		return msg;
	}

	public void setStudentid(Integer studentid) {
		this.studentid = studentid;
	}

	public void setSubjectManager(SubjectManager subjectManager) {
		this.subjectManager = subjectManager;
	}

	public OaStudent getStudent() {
		return student;
	}

	public void setStudent(OaStudent student) {
		this.student = student;
	}

	public void setLimit(int limit) {
		this.limit = limit;
	}

	public void setStart(int start) {
		this.start = start;
	}

	public int getTotal() {
		return total;
	}

	public void setQuery(String query) {
		this.query = query;
	}

	public void setContractManager(ContractManager contractManager) {
		this.contractManager = contractManager;
	}

	public List<String> getContracts() {
		return contracts;
	}

	public void setContracts(List<String> contracts) {
		this.contracts = contracts;
	}

	public void setUserManager(UserManager userManager) {
		this.userManager = userManager;
	}

	public void setQuery2(String query2) {
		this.query2 = query2;
	}

	public void setQuery3(String query3) {
		this.query3 = query3;
	}

	public void setQuery4(String query4) {
		this.query4 = query4;
	}

	public Integer getClassid() {
		return classid;
	}

	public void setClassid(Integer classid) {
		this.classid = classid;
	}

	public Integer getLeslesson() {
		return leslesson;
	}

	public void setLeslesson(Integer leslesson) {
		this.leslesson = leslesson;
	}

	public List<SubjectVO> getSubjects() {
		return subjects;
	}

	public void setSubjects(List<SubjectVO> subjects) {
		this.subjects = subjects;
	}

	public Double getSumoverage() {
		return sumoverage;
	}

	public Integer getSumlesson() {
		return sumlesson;
	}

	public List<TermVO> getTerms() {
		return terms;
	}

	public void setTerms(List<TermVO> terms) {
		this.terms = terms;
	}
}
